package com.hwl.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwl.server.entity.Employee;
import com.hwl.server.entity.MailLog;
import com.hwl.server.mapper.EmployeeMapper;
import com.hwl.server.mapper.MailLogMapper;
import com.hwl.server.pojo.EmployeePageParam;
import com.hwl.server.pojo.RespBean;
import com.hwl.server.pojo.RespPageBean;
import com.hwl.server.service.EmployeeService;
import com.hwl.server.utils.MailConstants;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author long
 * @since 2021-11-28
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private MailLogMapper mailLogMapper;

    @Override
    public RespPageBean getEmployeeByPage(EmployeePageParam pageParam) {
        Page<Employee> page = new Page<>(pageParam.getCurrentPage(), pageParam.getSize());

        IPage<Employee> employeeByPage = baseMapper.getEmployeeByPage(page, pageParam);
        return new RespPageBean(employeeByPage.getTotal(), employeeByPage.getRecords());
    }

    @Override
    public RespBean maxWorkId() {
        List<Map<String, Object>> maps = baseMapper.selectMaps(
                new QueryWrapper<Employee>().select("max(workID)"));

        return RespBean.success(null, String.format("%08d",
                Integer.parseInt(maps.get(0).get("max(workID)").toString()) + 1));
    }

    @Override
    public RespBean addEmp(Employee employee) {
        //处理合同期限，保留2位小数
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days / 365.00)));

        if (1 == baseMapper.insert(employee)) {
            Employee emp = baseMapper.getEmployee(employee.getId()).get(0);
            //消息落库
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog()
                    .setMsgId(msgId)
                    .setEid(emp.getId())
                    .setStatus(MailConstants.DELIVERING)
                    .setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME)
                    .setExchange(MailConstants.MAIL_EXCHANGE_NAME)
                    .setCount(0)
                    .setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT))
                    .setCreateTime(LocalDateTime.now())
                    .setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);

            //发送信息
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME,
                    emp, new CorrelationData(msgId));
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @Override
    public List<Employee> getEmployee(Integer id) {
        return baseMapper.getEmployee(id);
    }

    @Override
    public RespPageBean getEmployeeWithSalary(Integer currentPage, Integer size) {
        Page<Employee> page = new Page<>(currentPage, size);
        IPage<Employee> employeeIPage = baseMapper.getEmployeeWithSalary(page);
        return new RespPageBean(employeeIPage.getTotal(), employeeIPage.getRecords());
    }
}
